package com.udesc.larva.delta.ui;

import com.udesc.larva.delta.ui.vo.CorrespondenceAreaTypeEnum;
import com.udesc.larva.delta.ui.vo.CorrespondenceDistanceTypeEnum;
import com.udesc.larva.delta.ui.vo.CorrespondenceInterfaceParameter;

/**
 *
 * @author leobittencourt
 */
public class InterfaceCorrespondence extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOK;
    private javax.swing.ButtonGroup correspondenceAreaGroup;
    private javax.swing.ButtonGroup correspondenceFXGroup;
    private javax.swing.JLabel deltaLabel;
    private javax.swing.JTextField deltaValue;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel optionPanelCorrespondence;
    private javax.swing.JLabel rLabel;
    private javax.swing.JTextField rValue;
    private javax.swing.JRadioButton rbBoundingCircle;
    private javax.swing.JRadioButton rbBoundingBox;
    private javax.swing.JRadioButton rbDeltaCon;
    private javax.swing.JRadioButton rbPerimeter;
    private javax.swing.JRadioButton rbR2;
    private javax.swing.JRadioButton rbR3;
	
    private InterfaceAction action;
    
    public InterfaceCorrespondence(InterfaceAction action) {
        initComponents();
        this.action = action;
        this.rValue.setEnabled(false);
    }

    private void initComponents() {

        correspondenceAreaGroup = new javax.swing.ButtonGroup();
        correspondenceFXGroup = new javax.swing.ButtonGroup();
        optionPanelCorrespondence = new javax.swing.JPanel();
        rbDeltaCon = new javax.swing.JRadioButton();
        rbBoundingBox = new javax.swing.JRadioButton();
        rbBoundingCircle = new javax.swing.JRadioButton();
        rbPerimeter = new javax.swing.JRadioButton();
        buttonOK = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        rbR2 = new javax.swing.JRadioButton();
        rbR3 = new javax.swing.JRadioButton();
        deltaValue = new javax.swing.JTextField();
        deltaLabel = new javax.swing.JLabel();
        rLabel = new javax.swing.JLabel();
        rValue = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Correspondence Option");

        optionPanelCorrespondence.setBorder(javax.swing.BorderFactory.createTitledBorder("Choose an option"));

        correspondenceAreaGroup.add(rbDeltaCon);
        rbDeltaCon.setSelected(true);
        rbDeltaCon.setText("Default (Delta Connection)");
        rbDeltaCon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDeltaConActionPerformed(evt);
            }
        });

        correspondenceAreaGroup.add(rbBoundingBox);
        rbBoundingBox.setText("Bounding Box Area");
        rbBoundingBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbBoundingBoxActionPerformed(evt);
            }
        });

        correspondenceAreaGroup.add(rbBoundingCircle);
        rbBoundingCircle.setText("Bounding Circle Area");
        rbBoundingCircle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbBoundingAreaActionPerformed(evt);
            }
        });

        correspondenceAreaGroup.add(rbPerimeter);
        rbPerimeter.setText("Perimeter");
        rbPerimeter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPerimeterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout optionPanelCorrespondenceLayout = new javax.swing.GroupLayout(optionPanelCorrespondence);
        optionPanelCorrespondence.setLayout(optionPanelCorrespondenceLayout);
        optionPanelCorrespondenceLayout.setHorizontalGroup(
            optionPanelCorrespondenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPanelCorrespondenceLayout.createSequentialGroup()
                .addGroup(optionPanelCorrespondenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbDeltaCon)
                    .addComponent(rbBoundingBox)
                    .addComponent(rbBoundingCircle)
                    .addComponent(rbPerimeter))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        optionPanelCorrespondenceLayout.setVerticalGroup(
            optionPanelCorrespondenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPanelCorrespondenceLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rbDeltaCon)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbPerimeter)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(rbBoundingBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbBoundingCircle)
                .addContainerGap())
        );

        buttonOK.setText("OK");
        buttonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOKActionPerformed(evt);
            }
        });

        buttonCancel.setText("Cancelar");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Choose a distance option"));

        correspondenceFXGroup.add(rbR2);
        rbR2.setText("2D");
        rbR2.setSelected(true);

        correspondenceFXGroup.add(rbR3);
        rbR3.setText("3D");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbR2)
                    .addComponent(rbR3))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(rbR2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbR3))
        );

        deltaLabel.setText("Delta (%) (*):");

        rLabel.setText("R (%):");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionPanelCorrespondence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rLabel)
                            .addComponent(deltaLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(deltaValue, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
                            .addComponent(rValue, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optionPanelCorrespondence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(deltaLabel)
                            .addComponent(deltaValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rLabel)
                            .addComponent(rValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOK)
                    .addComponent(buttonCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    private void rbBoundingAreaActionPerformed(java.awt.event.ActionEvent evt) {                                               
        this.rbR2.setEnabled(true);
        this.rbR3.setEnabled(true);
        this.rValue.setEnabled(true);
        this.rbR2.setSelected(true);
}                                              

    private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
    	CorrespondenceInterfaceParameter parameterInterfaceCorrespondence = new CorrespondenceInterfaceParameter();
		
		if(this.rbBoundingCircle.isSelected()){
			parameterInterfaceCorrespondence.setCorrespondenceAreaType(CorrespondenceAreaTypeEnum.BOUNDING_CIRCLE_AREA);
		}else if(this.rbBoundingBox.isSelected()){
			parameterInterfaceCorrespondence.setCorrespondenceAreaType(CorrespondenceAreaTypeEnum.BOUNDING_BOX_AREA);
		}else if(this.rbDeltaCon.isSelected()){
			parameterInterfaceCorrespondence.setCorrespondenceAreaType(CorrespondenceAreaTypeEnum.DEFAULT_DELTA);
		}else if(this.rbPerimeter.isSelected()){
			parameterInterfaceCorrespondence.setCorrespondenceAreaType(CorrespondenceAreaTypeEnum.PERIMETER);
		}
		
		if(this.rbR2.isSelected()){
			parameterInterfaceCorrespondence.setDistanceType(CorrespondenceDistanceTypeEnum.R2);
		}else{
//			if(this.rbMultiply.isSelected()){
			parameterInterfaceCorrespondence.setDistanceType(CorrespondenceDistanceTypeEnum.R3);
		}
		
		double delta = java.lang.Double.parseDouble(this.deltaValue.getText());
		parameterInterfaceCorrespondence.setDelta(delta);
		
		if(this.rValue.getText() != null && !this.rValue.getText().equals("") )
			parameterInterfaceCorrespondence.setRange(java.lang.Double.parseDouble(this.rValue.getText()));
		else
			parameterInterfaceCorrespondence.setRange(0d);
		
		this.action.setParametroCorrespondence(parameterInterfaceCorrespondence);
		InterfaceTiling interfaceTiling = new InterfaceTiling(this.action);
		interfaceTiling.setVisible(true);
		this.setVisible(false);
    	
    }                                        

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {                                             
        this.setVisible(false);
    }                                            

    private void rbDeltaConActionPerformed(java.awt.event.ActionEvent evt) {
        this.rValue.setEnabled(false);
        this.rbR2.setSelected(true);
    }

    private void rbPerimeterActionPerformed(java.awt.event.ActionEvent evt) {
        this.rbR2.setEnabled(true);
        this.rbR3.setEnabled(true);
        this.rValue.setEnabled(true);
        this.rbR2.setSelected(true);
    }

    private void rbBoundingBoxActionPerformed(java.awt.event.ActionEvent evt) {
        this.rbR2.setEnabled(true);
        this.rbR3.setEnabled(true);
        this.rValue.setEnabled(true);
        this.rbR2.setSelected(true);
    }

}

